package cn.edu.stu.syllabus.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.edu.stu.syllabus.R;
import cn.edu.stu.syllabus.adapters.ThemePickerAdapter;
import cn.edu.stu.syllabus.datas.ThemeStyle;
import cn.edu.stu.syllabus.switcher.ThemeStyleSwitcher;
import cn.edu.stu.syllabus.utils.DensityUtil;
import cn.edu.stu.syllabus.utils.ScreenOriUtil;

/**
 * Created by daidaijie on 2017/3/8.
 */

public class ThemeDialog extends DialogFragment {

    public static final String DIALOG_THEME_PICKER = ThemeDialog.class.getCanonicalName();

    ThemePickerAdapter mThemePickerAdapter;

    @BindView(R.id.themePickerRecyclerView)
    RecyclerView mThemePickerRecyclerView;

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity())
                .inflate(R.layout.dialog_theme_picker, null, false);
        ButterKnife.bind(this, view);

        mThemePickerAdapter = new ThemePickerAdapter(ThemeStyle.ThemeStylesHolder.sThemeStyles);
        mThemePickerRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 5));
        mThemePickerRecyclerView.setAdapter(mThemePickerAdapter);
        mThemePickerRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                ThemeDialog.this.dismiss();
                ThemeStyle themeStyle = ThemeStyle.ThemeStylesHolder.sThemeStyles.get(position);
                ThemeStyleSwitcher.getInstance().setThemeStyle(themeStyle.name);
                ThemeStyleSwitcher.getInstance().switchThemeStyle((AppCompatActivity) getActivity());
            }
        });

        if (!ScreenOriUtil.isPortrait()) {
            int pxOf16dp = DensityUtil.dip2px(16);
            mThemePickerRecyclerView.setPadding(pxOf16dp * 2, pxOf16dp, 0, 0);
        }

        return new AlertDialog.Builder(getActivity())
                .setTitle("选择主题")
                .setView(view)
                .setPositiveButton("取消", null)
                .create();
    }
}
